﻿using System;
using System.Diagnostics;

public class OrderDetails
{
    public string goods_name;
    public int goods_price;
    public string cus;
    public OrderDetails(string goods_name, int goods_price, string cus)
    {
        this.goods_name = goods_name;
        this.goods_price = goods_price;
        this.cus = cus;
    }

    //Equal方法重载
    public override bool Equals(object obj)
    {
        OrderDetails details = obj as OrderDetails;
        return details!=null &&
               goods_name == details.goods_name &&
               goods_price == details.goods_price &&
               cus == details.cus;
    }

    //ToString方法重载
    public override string? ToString()
    {
        return "货物名："+goods_name+" 客户："+cus+" 订单金额："+goods_price;
    }
}

public class Order
{
    private int id;
    private OrderDetails OrderDetails;
    public int getid()
    {
        return id;
    }
    public string getname()
    {
        return OrderDetails.goods_name;
    }
    public string getcus()
    {
        return OrderDetails.cus;
    }
    public int getprice()
    {
        return OrderDetails.goods_price;
    }
    public void setOrderDetails(OrderDetails details)
    {
        OrderDetails = details;
    }
    Order(int id, string goods_name, int goods_price, string cus)
    {
        this.id = id;
        OrderDetails new_det = new OrderDetails(goods_name, goods_price, cus);
        this.OrderDetails = new_det;
    }

    //Equal方法重载
    public override bool Equals(object obj)
    {
        Order new_order = obj as Order;
        return new_order!=null &&
               id == new_order.id &&
               new_order.OrderDetails.Equals(OrderDetails);
    }

    //ToString方法重载
    public override string? ToString()
    {
        return "订单"+id+": "+OrderDetails.ToString();
    }
}

public class OrderService
{
    private List<Order> Order_list;
    public void add(Order order)
    {
        foreach(Order item in Order_list)
        {
            if (item.Equals(order)) return;//有重复订单信息时不加入
        }
        Order_list.Add(order);
    }

    //按照订单id排序
    public void sort()
    {
        var tmp = from order in Order_list orderby order.getid()
                  select order;
        Order_list.Clear();
        foreach(Order item in tmp)
        {
            Order_list.Add(item);
        }
    }

    //修改，找不到id时throw异常
    public void alter(int id, string goods_name, int goods_price, string cus)
    {
        foreach(Order order in Order_list)
        {
            if(order.getid()==id)
            {
                OrderDetails tmp = new OrderDetails(goods_name, goods_price, cus);
                order.setOrderDetails(tmp);
                return;
            }
        }
        throw (new AlterFailException("no such id can be altered..."));
    }

    //利用linq完成搜索
    public void search(string type,object data)
    {
        if(type=="id")
        {   
            int sel_id = Convert.ToInt32(data);
            var list = Order_list.Where(s => s.getid()==sel_id).ToList();
            foreach(var item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
        else if (type == "cus")
        {
            string cus = Convert.ToString(data);
            var list = Order_list.Where(s => s.getcus() == cus).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
        else if (type == "name")
        {
            string name = Convert.ToString(data);
            var list = Order_list.Where(s => s.getname() == name).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
        else if(type == "price")
        {
            int price = Convert.ToInt32(data);
            var list = Order_list.Where(s => s.getprice() == price).ToList();
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
    }

    //删除操作
    public void delete(int id)
    {
        foreach (Order order in Order_list)
        {
            if (id == order.getid())
            {
                Order_list.Remove(order);
                return;
            }
        }
        throw (new DeleteFailException("no such id can be delected..."));
    }
    
}

//异常类
public class AlterFailException:ApplicationException
{
    public AlterFailException(string message):base(message)
    {
    }
}

public class DeleteFailException : ApplicationException
{
    public DeleteFailException(string message) : base(message)
    {
    }
}

public class Program
{
    static void Main(string[] args)
    {

    }
        

}
